package lium.lbs.lbsim.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.buz.hjcdriver.bean.BankCardListResultBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_banklist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.BankListAdapter
import java.util.HashMap

class BankCardListActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {


    lateinit var adapter: BankListAdapter
    var page:Int=1

    var isChooseBankCard=false
    override fun getLayoutId(): Int { return R.layout.activity_banklist  }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("银行卡")
        if (intent!=null && intent.hasExtra("isChooseBankCard")){
            isChooseBankCard = intent.getBooleanExtra("isChooseBankCard",false)
        }
        initRecyclerView()
        addOnClickListeners(R.id.right_bar)
    }

    override fun onResume() {
        super.onResume()
        getDataList()
    }
    fun initRecyclerView(){
        //初始化adapter
        adapter = BankListAdapter()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter.isFooterViewAsFlow=true

        //设置emptyView
        adapter.emptyView = LayoutInflater.from(this).inflate(R.layout.view_list_nodata_banklist, null, false)
        adapter.emptyView.findViewById<LinearLayout>(R.id.addbackbtn).setOnClickListener(this)
        adapter.setOnItemClickListener(this)
        var footerview=LayoutInflater.from(this).inflate(R.layout.footerview_addbank, null, false)
        footerview.findViewById<LinearLayout>(R.id.addbackbtn).setOnClickListener(this)
        adapter.setFooterView(footerview)
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        refreshLayout.setOnRefreshListener {
            page =1
            getDataList()
        }
        refreshLayout.setOnLoadMoreListener {
            page =1
            getDataList()
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v!!.id){
            R.id.addbackbtn -> {
                val intent = Intent(this, BankCardAddActivity::class.java)
                startActivity(intent)
            }
        }
    }
    private fun getDataList() {
        val param = HashMap<String, String>()
        postData("/member/selectBankCardByMemberid",param, object : DialogCallback<ResponseBean<List<BankCardListResultBean>>>(this) {
            override fun onSuccess(response: Response<ResponseBean<List<BankCardListResultBean>>>) {
                setDataList(response.body().data)
            }
        })
    }

    private fun setDataList(list: List<BankCardListResultBean>) {
        if (page==1){
            adapter!!.data.clear()
        }
        refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()
        list?.let {
            adapter!!.data.addAll(list)
        }
        if (list==null || list.isEmpty()) {
            refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
        }
        refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
        adapter!!.notifyDataSetChanged()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        if (isChooseBankCard){
            adapter?.data!![position]?.let {
                var bean =adapter?.data!![position] as BankCardListResultBean
                var backIntent = Intent()
                backIntent.putExtra("bean",bean)
                setResult(Activity.RESULT_OK,backIntent)
                finish()
            }
        }
    }

    override fun getNetWorkData() {
    }


}